#include "log.h"

#include <sstream>
#include <ctime>

void LogPrint(LogLevel level, const char *szFmt, ...)
{
    std::string levelStr;
    switch (level)
    {
    case LOG_LEVEL_DEBUG:
        levelStr = "DEBUG";
        break;
    case LOG_LEVEL_INFO:
        levelStr = "INFO";
        break;
    case LOG_LEVEL_WARN:
        levelStr = "WARN";
        break;
    case LOG_LEVEL_ERROR:
        levelStr = "ERROR";
        break;
    }
    char logBuf[1024] = {0};
    va_list lst;
    va_start(lst, szFmt);
    vsprintf(logBuf, szFmt, lst);
    va_end(lst);

    std::time_t currentTime = std::time(nullptr);

    // 将时间转换为可读的字符串形式
    std::stringstream ss;
    ss << std::ctime(&currentTime);

    // 打印时间字符串
    std::string timeString = ss.str();
    // 去除换行符，‌因为std::ctime会在字符串末尾添加一个换行符
    timeString.pop_back();
    std::cout << timeString << " [" << levelStr << "]: " << logBuf << std::endl;
}